<?php
/**
 * 产品分类模型 
 *
 * @copyright   Copyright 2011, Richie
 * @author      Richie <richbossman@163.com>
 * @package     SMS
 * @link        http://www.letphp.com
 * 
 * $Id: CategoryModel.class.php 76 2011-04-07 14:09:03Z 442767452@qq.com $
 */
class CategoryModel extends SMSModel {
	//数据验证
	protected $_validate	=	array(
		array('name','checkName', '该分类名称已经存在！',0,'callback'),
		array('parent_id', 'checkParent', '所属上级分类不能为自己或自己的子级，请更改。', 2, 'callback')
	);
	//自动完成
	protected $_auto = array(
		array('add_time', 'time', 1, 'function'),
		array('update_time', 'time', 3, 'function'),
		array('status', '1')
	);
	
	//检查名称是否存在
	function checkName(){
		$map['name'] = $_POST['name'];
        if(!empty($_POST['id'])) {
			$map['id']	=	array('neq',$_POST['id']);
        }
		$result	=	$this->where($map)->field('id')->find();
        if($result) {
        	return false;
        }else{
			return true;
		}
	}

	//检查上级分类是否与自己相同
	function checkParent(){
		$parent_id = $_POST['parent_id'];
		$bool = (isset($_POST['id']) && ($_POST['id'] == $parent_id)) ? false : true;
		if($bool == false){
			return false;
		}
		if(!empty($_POST['id'])){
			if(in_array($parent_id, $this->childrenId($this->getAllCategory($_POST['id'])))){
				return false;
			}
		}
		return true;
	}
	
	//递归取所有分类
	function getAllCategory($id=0, $isall=true, $level=0){
		$cate = array();
		$map['parent_id'] = $id;
		($isall == false) ? $map['status']=1 : '';
		$cate = $this->where($map)->order('sort ASC, id ASC')->findAll();
		if($cate){
			$level++;
			foreach ($cate as $key=>$cates){
				$cate[$key]['add_time'] = date('Y-m-d H:i', $cates['add_time']);
				$cate[$key]['level'] = $level;
				$cate[$key]['status'] = ($cates['status'] == 1) ? '启用' : '禁用';
				$children = $this->getAllCategory($cates['id'], $isall, $level);
				if(!empty($children)) $cate[$key]['children'] = $children;
			}
		}
		return $cate;
	}
	
	//某分类的子类
	function childrenId($data=array()){
		static $childid=array();
		foreach($data as $cate){
			$childid[] = $cate['id'];
			if(!empty($cate['children'])){
				$this->childrenId($cate['children']);
			}
		}
		return $childid;
	}
	
	//递归取所有分类供tree使用
	function getAllCategoryForTree($id=0, $isall=true){
		$cate = array();
		$map['parent_id'] = $id;
		($isall == false) ? $map['status']=1 : '';
		$cate = $this->where($map)->order('sort ASC, id ASC')->field('id, name as text')->findAll();
		if($cate){
			foreach ($cate as $key=>$cates){
				$children = $this->getAllCategoryForTree($cates['id'], $isall);
				if(!empty($children)) $cate[$key]['children'] = $children;
			}
		}
		return $cate;
	}
	
	//取得属性位置
	function getCategoryPosition($id=0){
		static $attribute = array();
		$parent = $this->where('id='.$id)->field('id,parent_id,name')->find();
		if($parent){
			$attribute[] = $parent['name'];
		}
		if($parent['parent_id']){
			$this->getCategoryPosition($parent['parent_id'], true);
		}
		return implode(" > ", array_reverse($attribute));
	}
	
	//取得分类信息
	function getCategory($id=0){
		if(empty($id)) return array();
		return $this->find($id);
	}
}
?>